layui.use(['table','form','jquery', 'backend'],function () {
    var table = layui.table;
    var form = layui.form;
    var $ = layui.jquery;
    var backend = layui.backend;

    var cols = [
        [
            {type: 'radio'},
            {title: '路径', field: 'url', align:'center'},
            {title: '文件名', field: 'filename', align:'center'},
            {title: '文件大小', field: 'filesize', align:'center'},
            {title: '类型', field: 'mimetype', align:'center'},
            {title: '位置', field: 'storage', align:'center'},
            {title: '文件 sha1编码', field: 'sha1', align:'center'},
            {title: '操作', toolbar: '#bar', align:'center', width:195}
        ]
    ];

    table.render({
        elem: '#table',
        url: '/admin/attachment/select',
        page: true ,
        cols: cols ,
        skin: 'line',
        toolbar: '#toolbar',
        parseData : function (res) {
            return {
                "code": res.code == 1 ? 0 : res.code, //解析接口状态
                "msg": res.msg, //解析提示文本
                "count": res.data.total, //解析数据长度
                "data": res.data.list //解析数据列表
            };
        },
        defaultToolbar: [{
            title: '刷新',
            layEvent: 'refresh',
            icon: 'layui-icon-refresh',
        }, 'filter', 'print', 'exports']
    });

    table.on('tool(table)', function(obj){
        if(obj.event === 'remove'){
            window.remove(obj);
        } else if(obj.event === 'edit'){
            window.edit(obj);
        }
    });

    table.on('toolbar(table)', function(obj){
        if(obj.event === 'add'){
            window.add();
        } else if(obj.event === 'refresh'){
            table.reload('table');
        } else if(obj.event === 'batchRemove'){
            window.batchRemove(obj);
        } else if(obj.event === 'select'){
            window.doselect(obj);
        }
    });

    form.on('submit(query)', function(data){
        table.reload('table',{where:data.field})
        return false;
    });

    window.add = function(){
        backend.api.open('/admin/attachment/show', '上传');
    };

    window.remove = function(obj){
        layer.confirm('确定要删除该附件吗？', {icon: 3, title:'提示'}, function(index){
            layer.close(index);
            var loading = layer.load();
            $.ajax({
                url: "/admin/attachment/del",
                dataType:'json',
                type:'post',
                data : {ids : [obj.data['roleId']]},
                success:function(result){
                    layer.close(loading);
                    if(result.code == 1){
                        layer.msg(result.msg,{icon:1,time:1000},function(){
                            table.reload('table');
                        });
                    }else{
                        layer.confirm(res.msg, {icon: 3, title:'警告'}, function(index){
                            layer.close(index);
                        });
                    }
                }
            })
        });
    };

    window.batchRemove = function(obj){
        var data = table.checkStatus(obj.config.id).data;
        if(data.length === 0){
            layer.msg("未选中数据",{icon:3,time:1000});
            return false;
        }
        var ids = "";
        for(var i = 0;i<data.length;i++){
            ids += data[i].userId+",";
        }
        ids = ids.substr(0,ids.length-1);
        layer.confirm('确定要删除这些附件?', {icon: 3, title:'提示'}, function(index){
            layer.close(index);
            var loading = layer.load();
            $.ajax({
                url: "/admin/attachment/del",
                dataType:'json',
                type:'post',
                data : {ids : ids},
                success:function(result){
                    layer.close(loading);
                    if(result.success){
                        layer.msg(result.msg,{icon:1,time:1000},function(){
                            table.reload('table');
                        });
                    }else{
                        layer.confirm(res.msg, {icon: 3, title:'警告'}, function(index){
                            layer.close(index);
                        });
                    }
                }
            })
        });
    }

    window.doselect = function (obj) {
        var data = table.checkStatus(obj.config.id).data;
        if(data.length === 0){
            layer.msg("未选中数据",{icon:3,time:1000});
            return false;
        }
        backend.api.close(data)
    }
})
